<?php

declare(strict_types=1);

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 * 用户账号
 */
class User extends Model
{
    protected $connection = 'mysql';
    /**
     * 用户表
     */
    protected $table = 'user';

    const REG_TYEPS = [
        0 => '手机号',
        1 => 'QQ',
        2 => '微信'
    ];

    const APP_NAME = [
        1 => '极速聊',
        2 => '随心聊',
        3 => '天天一起聊',
        4 => '一对漂流瓶',
        6 => '约遇',
        8 => '友聊',
        9 => '来聊'
    ];

    //客服
    const CHANNEL_TYPE = [
        409 => '官方',
        405 => '约遇-小米',
        404 => '约遇-oppo',
        402 => '约遇-华为',
        403 => '约遇-vivo',
        408 => '默认',
        501 => '缘友-应用宝',
        401 => '约遇-应用宝',
        506 => '邀请渠道',
        410 => '新浪-微博'
    ] ;

    /**
     * 用户状态
     */
    const STATUS_OK = 0;
    const STATUS_FREEZE = 1;
    const STATUS_CANCELLATTED = 2;
    const STATUS_FORBID = 3;
    const STATUS = [
        self::STATUS_OK => '正常',
        self::STATUS_FREEZE => '冻结',
        self::STATUS_CANCELLATTED => '已注销',
        self::STATUS_FORBID => '禁言'
    ];

    /**
     * 注册系统
     */
    const PLATFORM_IOS = 1;
    const PLATFORM_ANDROID = 2;
    const PLATFORM = [
        self::PLATFORM_IOS => 'IOS',
        self::PLATFORM_ANDROID => '安卓',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_name', 'phone', 'version', 'device_id', 'client_ip', 'platform', 'android_market', 'device', 'pwd', "im_account", "im_token", "status", "official"
    ];

    public function userProfile()
    {
        return $this->hasOne(UserProfile::class, 'user_id');
    }

    public function userCoin()
    {
        return $this->hasOne(UserCoin::class, 'user_id');
    }

    public function appChannel()
    {
        return $this->hasOne(AppChannel::class, 'channel', 'channel');
    }

    public function userScore()
    {
        return $this->hasOne(UserScore::class,'user_id');
    }

    /**
     * 根据用户ID获取IM账号
     */
    static function getImAccounts($userIds = [], $statusValid = true)
    {
        if (!$userIds) return [];
        if (!is_array($userIds)) $userIds = [$userIds];
        $where = [
            ['im_account', '<>', '']
        ];
        if ($statusValid) $where['status'] = User::STATUS_OK;
        return self::where($where)->whereIn('id', $userIds)->pluck('im_account', 'id')->toArray();
    }

    /**
     * 渠道注册人数
     */
    static function getChannelNum($channel = '', $start = '', $end = '')
    {
        $where = [];
        if ($start) $where[] = ['created_at', '>=', $start];
        if ($end) $where[] = ['created_at', '<', $end];
        if ($channel) $where[] = ['channel', $channel];
        else return self::where($where)->count();
    }

    /**
     * 注销账号
     */
    static function cancelAccount($userId = 0)
    {
        //第三方解除绑定
        SocialAccount::where('user_id', $userId)->limit(1)->delete();
        //更新手机号
        $record = User::find($userId);
        $cancelPhone = $record->phone . '-X';
        $upInfo = [
            'phone' => $cancelPhone,
            'user_name' => $cancelPhone,
            'status' => self::STATUS_CANCELLATTED
        ];
        return $record->update($upInfo);
    }
}
